package com.sinothk.special.buyer.userblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.special.buyer.userblog.entity.UserBlog;
import com.sinothk.special.buyer.userblog.entity.UserBlogAddVO;
import com.sinothk.special.buyer.userblog.entity.UserBlogDTO;
import com.sinothk.special.buyer.userblog.entity.UserBlogSearchVO;
import com.sinothk.special.buyer.userblog.service.IUserBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 个人博客
 * @Author: Liangyt
 * @Date: 2022-03-30
 * @Version: V1.0
 */
@Api(tags = "个人博客")
@RestController
@RequestMapping("/blog/userBlog")
@Slf4j
public class UserBlogController extends JeecgController<UserBlog, IUserBlogService> {
    @Autowired
    private IUserBlogService userBlogService;

    /**
     * 查询-Web
     *
     * @param userBlog
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "查询-Web")
    @ApiOperation(value = "查询-Web", notes = "查询-Web")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(UserBlog userBlog,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<UserBlog> queryWrapper = QueryGenerator.initQueryWrapper(userBlog, req.getParameterMap());
        Page<UserBlog> page = new Page<UserBlog>(pageNo, pageSize);
        IPage<UserBlog> pageList = userBlogService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 查询-附近热门博客
     *
     * @param searchVO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "查询-附近热门博客")
    @ApiOperation(value = "查询-附近热门博客", notes = "查询-附近热门博客")
    @GetMapping(value = "/listBlogsForNearbyAndHot")
    public Result<IPage<UserBlogDTO>> listBlogsForNearbyAndHot(
            UserBlogSearchVO searchVO,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return Result.OK(userBlogService.listForNearbyAndHotBlogs(new Page<>(pageNo, pageSize), searchVO));
    }

    /**
     * 查询-个人博客
     *
     * @param searchVO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "查询-个人博客")
    @ApiOperation(value = "查询-个人博客", notes = "查询-个人博客")
    @GetMapping(value = "/listBlogsForUser")
    public Result<IPage<UserBlogDTO>> listBlogsForUser(
            UserBlogSearchVO searchVO,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        return Result.OK(userBlogService.listBlogsForUser(new Page<>(pageNo, pageSize), searchVO));
    }

    /**
     * 查询-好友博客
     *
     * @param searchVO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "查询-好友博客")
    @ApiOperation(value = "查询-好友博客", notes = "查询-好友博客")
    @GetMapping(value = "/listBlogsForILike")
    public Result<IPage<UserBlogDTO>> listBlogsForILike(
            UserBlogSearchVO searchVO,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        return Result.OK(userBlogService.listBlogsForILike(new Page<>(pageNo, pageSize), searchVO));
    }

    /**
     * 添加
     *
     * @param userBlogVo
     * @return
     */
    @AutoLog(value = "个人博客-添加")
    @ApiOperation(value = "个人博客-添加", notes = "个人博客-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody UserBlogAddVO userBlogVo) {
        userBlogService.saveVo(userBlogVo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param userBlog
     * @return
     */
    @AutoLog(value = "个人博客-编辑")
    @ApiOperation(value = "个人博客-编辑", notes = "个人博客-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody UserBlog userBlog) {
        userBlogService.updateById(userBlog);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "个人博客-通过id查询")
    @ApiOperation(value = "个人博客-通过id查询", notes = "个人博客-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        UserBlog userBlog = userBlogService.getById(id);
        if (userBlog == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(userBlog);
    }
}
